package services

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"todo-demo/client"
	"todo-demo/model"
)

type TodoService struct {
}

func (t TodoService) Index(c *gin.Context) {
	recordList := []model.Record{}
	client.MysqlClient{}.LoadRecord(&recordList)

	//for idx, record := range recordList {
	//	fmt.Println(idx, record)
	//}
	c.HTML(200, "todo/index.html", gin.H{
		"records": recordList,
	})

}

func (t TodoService) AutoMigrate(c *gin.Context) {
	fmt.Println("TodoService.AutoMigrate")
	client.MysqlClient{}.Migrate()
	c.Redirect(302, "/todo")
}

func (t TodoService) Add(c *gin.Context) {
	record := model.Record{}
	c.Bind(&record)
	go client.MysqlClient{}.Add(&record)
	fmt.Printf("%#v ", record)
	c.Redirect(302, "/todo")
}

func (t TodoService) Finish(c *gin.Context) {
	recordId := c.Query("id")
	fmt.Println("update id is ", recordId)
	client.MysqlClient{}.Finish(recordId)
	c.Redirect(302, "/todo")
}
